
// 生成树结构菜单
const generateOptions = (params) => {
  let result = [];
  for (let param of params) {
    if (param.pid == 0) {//判断是否为顶层节点
      param.children = getchildsID(param.id, params);//获取子节点
      result.push(param);
    }
  }
  return result;
}

// 遍历子元素
const getchildsID = (id, array) => {
  let childs = new Array();
  for (let arr of array) {
    if (arr.pid == id) {
      childs.push(arr)
    }
  }
  for (let child of childs) {//获取子节点的子节点
    let childscopy = getchildsID(child.id, array)//递归获取子节点
    if (childscopy.length > 0) {
      child.children = childscopy;
    }
  }
  return childs
}


/**
 * 去除数组中重复的对象
 * @param {*} obj 
 */
const uniqueArr = (obj) => {
  var uniques = [];
  var stringify = {};
  for (var i = 0; i < obj.length; i++) {
    var keys = Object.keys(obj[i]);
    keys.sort(function (a, b) {
      return (Number(a) - Number(b));
    });
    var str = '';
    for (var j = 0; j < keys.length; j++) {
      str += JSON.stringify(keys[j]);
      str += JSON.stringify(obj[i][keys[j]]);
    }
    if (!stringify.hasOwnProperty(str)) {
      uniques.push(obj[i]);
      stringify[str] = true;
    }
  }
  uniques = uniques;
  return uniques;
}










module.exports = {
  generateOptions,
  getchildsID,
  uniqueArr
}